package com.example.definedview.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.definedview.R;

import java.util.Calendar;

import java.util.logging.LogRecord;

public class ClockView extends View {
    Paint paint;//画笔
    int curHours, curMinutes, curSeconds;//时分秒
    int tarHours, tarMinutes, tarSeconds;//目标时分秒
    int deg = 0;//秒针旋转的角度
    int time = 0;

    //用于在activity中直接创建View对象
    public ClockView(Context context) {
        super(context);
    }

    //用于布局的使用
    public ClockView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(getResources().getColor(R.color.clock));
    }

    public int getTarHours() {
        return tarHours;
    }

    public void setTarHours(int tarHours) {
        this.tarHours = tarHours;
    }

    public int getTarMinutes() {
        return tarMinutes;
    }

    public void setTarMinutes(int tarMinutes) {
        this.tarMinutes = tarMinutes;
    }

    public int getTarSeconds() {
        return tarSeconds;
    }

    public void setTarSeconds(int tarSeconds) {
        this.tarSeconds = tarSeconds;
    }

    //获取倒计时的时间
    public void getTime(){
        Calendar calendar = Calendar.getInstance();
        curHours = calendar.get(Calendar.HOUR);
        curMinutes = calendar.get(Calendar.MINUTE);
        curSeconds = calendar.get(Calendar.SECOND);

    }

    //显示的内容在onDraw中绘制，提供了一个canvas对象，即画布，在画布上绘制内容
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setStyle(Paint.Style.STROKE);//设置空心
        paint.setStrokeWidth(8);//设置线条宽度
        setPadding(20,20,20,20);//设置内边距
        canvas.drawCircle(getWidth()/2, getHeight()/2, getWidth()/2-20, paint);//绘制外层的大圆
        paint.setStrokeWidth(4);
        canvas.drawCircle(getWidth()/2, getHeight()/2, getWidth()/2-30, paint);//内层的圆
        paint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(getWidth()/2, getHeight()/2, 10, paint);//绘制轴心
        //绘制刻度线
        for(int i=1; i<=12; i++){
            //保存画布状态
            canvas.save();
            //画布旋转到指定角度
            canvas.rotate(360/12*i, getWidth()/2, getHeight()/2);
            canvas.drawLine(getWidth()/2, 40, getWidth()/2, 50, paint);
            //恢复旋转之前的状态
            canvas.restore();
        }

        //绘制秒针
        paint.setStrokeWidth(5);
        canvas.save();
        System.out.println("--------------------------------------------------------------------deg:" + deg);
        canvas.rotate(deg, getWidth()/2, getHeight()/2);
        canvas.drawLine(getWidth()/2, getHeight()/2, getWidth()/2, getHeight()/2 - getHeight()/3, paint);
        canvas.restore();

    }


    //尺寸的测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        //固定的流程
        //1.获得宽高的模式(match parent, wrap content, 具体值)
        int wmode = MeasureSpec.getMode(widthMeasureSpec);
        int hmode = MeasureSpec.getMode(heightMeasureSpec);
        //2.获取宽度和高度的最大尺寸
        int wsize = MeasureSpec.getSize(widthMeasureSpec);
        int hsize = MeasureSpec.getSize(heightMeasureSpec);
        //3.判断模式
        int size = 400;
        if(wmode == MeasureSpec.EXACTLY){
            //宽度在布局时设定为精确值
            if(hmode == MeasureSpec.EXACTLY){
                //高度在布局时设定为精确值
                size = Math.min(wsize, hsize);
            }else{
                size = wsize;
            }
        }else{
            if(hmode == MeasureSpec.EXACTLY){
                size = hsize;
            }else{
                size = 400;
            }
        }

        //4.将测量好的值传给宽高
        setMeasuredDimension(size,size);
    }

    public ValueAnimator antiClockwise(int tarSeconds){
        final int degree = tarSeconds * 6;
        ValueAnimator startAnimator = ValueAnimator.ofFloat(0, 1);
        startAnimator.setDuration(500);//动画时长
        startAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); //插值器，又名加速器
        startAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float t = (Float) animation.getAnimatedValue();//拿到变化值,ValueAnimator.ofFloat(0, 1),所以变化值在(0,1)之间

                deg = (int) ((float)degree * t);//计算秒针旋转的角度
                System.out.println("--------------------------intoAntiClock, deg="+deg);

                invalidate(); //通知界面重绘
            }
        });
        startAnimator.start();
        return startAnimator;
    }

    public void startClock(int time){
        this.time = time;

        System.out.println("------------------------------------cur:"+time);

        deg = time % 60 * 6;
        handler.sendEmptyMessage(1);
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if(msg.what == 1){
                getTime();
                //int cur = (tarHours*3600 + tarMinutes*60 + tarSeconds) - (curHours*3600 + curMinutes*60 + curSeconds);

                if(time >= 0){
                    deg = time % 60 * 6;
                    time -- ;
                    invalidate();
                    handler.sendEmptyMessageDelayed(1, 1000);
                }

            }
        }
    };

}
